package com.qikux.controller;

import cn.hutool.captcha.AbstractCaptcha;
import com.qikux.annotation.Component;
import com.qikux.annotation.RequestMapping;
import com.qikux.consts.AppConst;
import com.qikux.utils.CodeUtils;
import com.qikux.utils.ContextUtils;

import javax.servlet.ServletOutputStream;

@Component
@RequestMapping("")
public class CodeController {

    @RequestMapping("/code")
    public void generatorCode() throws Exception{

        // 获取 宽和高
        int width = Integer.parseInt(ContextUtils.getRequest().getParameter("w")) ;
        int height = Integer.parseInt(ContextUtils.getRequest().getParameter("h")) ;

        AbstractCaptcha captcha = CodeUtils.createCaptcha(width, height);

        // 获取验证码 ，存储到 Session中
        String code = captcha.getCode();

        ContextUtils.getSession().setAttribute(AppConst.CODE , captcha);

        // 将验证码通过流的技术，写入到浏览器中
        ServletOutputStream outputStream = ContextUtils.getResponse().getOutputStream();

        captcha.write(outputStream);

    }
}
